<?php
// 本类由系统自动生成，仅供测试用途
class WxMenuAction extends CommonAction {

	public function index(){
		$profile_id=$_SESSION['profile_id'];
		$this->myApps=M("app")->where("id in(select appid from wx_user_app where userid=%d)",$profile_id)->select();
		$db=D("Graphic");
		$this->graphics=$db->relation(true)->where("create_user_id =%d",$profile_id)->select();
		$profile=M("profile")->where(" id=%d ",$profile_id)->find();
		$access_token=$_SESSION['access_token'];
		$expires_in=$_SESSION['expires_in'];
		$ts=$_SESSION['ts'];
		if(empty($access_token)){
			if(!empty($profile["appid"])&&!empty($profile["secret"])){
				$this->getTokenTosession($profile["appid"],$profile["secret"]);
				$_SESSION['profile_appid']	=$profile["appid"];$_SESSION['profile_secret']	=$profile["secret"];

			}else {
				die("error");
			}

		}else{
			if($this->checkTs($ts)){
				if(!empty($profile["appid"])&&!empty($profile["secret"])){
					$this->getTokenTosession($profile["appid"],$profile["secret"]);
					$_SESSION['profile_appid']	=$profile["appid"];$_SESSION['profile_secret']	=$profile["secret"];
				}else {
					die("error");
				}
			}
		}
		$this->display();
	}

	private function getTokenTosession($appid="",$secret=""){
		$url = C('WEIXIN_API_URL')."/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
		$obj=json_decode(getFromUrl($url),true);
		$_SESSION['access_token']	=	$obj['access_token'];
		$_SESSION['expires_in']	=	$obj['expires_in'];
		$_SESSION['ts']	=time();
	}

	private function checkTs($ts=0){
		$now=time();
		$result=$now-$ts;
		if($result>60*60){
			return true;
		}else{
			return  false;
		}
	}
	/***
	 * 从微信查询菜单
	*/
	public function get_menu() {
		$appid=$_SESSION['profile_appid'];
		$secret=$_SESSION['profile_secret'];
		$url = C('WEIXIN_API_URL')."/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
		$obj=json_decode(getFromUrl($url),true);
		$_SESSION['access_token']	=	$obj['access_token'];
		$_SESSION['expires_in']	=	$obj['expires_in'];
		$_SESSION['ts']	=time();
		$url = C('WEIXIN_API_URL')."/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
		$obj=json_decode(getFromUrl($url),true);

		$path =C('WEIXIN_API_URL').C('GET_MENU').$obj["access_token"];
		echo getFromUrl($path);
	}

	public function resume_menu(){
		$profile_id=19;//先写死
		$profile=M("profile")->where(" id=%d ",$profile_id)->find();
		echo $profile['menuBackupData'];
	}

	public function create_menu(){
		$profile_id=$_SESSION['profile_id'];
		$profile=M("profile")->where(" id=%d ",$profile_id)->find();
		if(!empty($profile["appid"])&&!empty($profile["secret"])){
			$this->getTokenTosession($profile["appid"],$profile["secret"]);
			$_SESSION['profile_appid']	=$profile["appid"];
			$_SESSION['profile_secret']	=$profile["secret"];
			$json=I($json,null);
			if(is_null($json)){
				die("传入参数有错");
			}else{
				$url = C('WEIXIN_API_URL').C('CRETAE_MENU').$profile["secret"];
				$response=getFromUrl($url,$json);
				if(!is_null($response)){
					$profile=M("profile")->where(" id=%d ",$profile_id)->find();
					$profile["menuBackupData"]=$json;
					M("profile")->data($profile)->save();
					echo $response;
				}
			}
		}else {
			die("error");
		}
	}

}